package org.giovanolli.mumi.controller.business;

import java.util.Date;

import org.giovanolli.mumi.controller.AbstractController;
import org.giovanolli.mumi.controller.AbstractModel;
import org.giovanolli.mumi.controller.UseCaseMode;
import org.giovanolli.mumi.entities.GeneralObservation;
import org.giovanolli.mumi.entities.Patient;
import org.giovanolli.mumi.entities.PatientEvaluation;
import org.giovanolli.mumi.entities.filter.PatientEvaluationFilter;
import org.giovanolli.mumi.entities.filter.PatientFilter;
import org.giovanolli.mumi.entities.filter.SpecialistFilter;
import org.giovanolli.mumi.exception.NotImplementedMethodException;
import org.giovanolli.mumi.service.PatientEvaluationService;
import org.giovanolli.mumi.service.PatientService;
import org.giovanolli.mumi.service.Service;
import org.giovanolli.mumi.service.SpecialistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * The Class PatientEvaluationController.
 * 
 * @author root
 */
@Controller("patientEvaluationController")
public class PatientEvaluationController extends AbstractController<PatientEvaluation> {

	/** The service. */
	@Autowired
	private PatientEvaluationService service;

	/** The patient service. */
	@Autowired
	private PatientService patientService;

	/** The specialist service. */
	@Autowired
	private SpecialistService specialistService;

	/** The model. */
	@Autowired
	private PatientEvaluationModel model;

	/**
	 * Observation Events controller.
	 */
	public void addObservationEvent() {
		model.setEditedObservation(new GeneralObservation());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#bindEntityToModel()
	 */
	@Override
	protected void bindEntityToModel() {
		PatientEvaluation editedPatientEvaluation = model.getEditedEntity();
		model.setFrom(editedPatientEvaluation.getFromDate());
		model.setTo(editedPatientEvaluation.getToDate());
		model.setPatient(editedPatientEvaluation.getPatient());
		model.setSpecialist(editedPatientEvaluation.getSpecialist());
		model.setEvaluation(editedPatientEvaluation.getEvaluation());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#bindModelToEntity()
	 */
	@Override
	protected void bindModelToEntity() {
		PatientEvaluation editedPatientEvaluation = model.getEditedEntity();
		editedPatientEvaluation.setFromDate(model.getFrom());
		editedPatientEvaluation.setToDate(model.getTo());
		editedPatientEvaluation.setPatient(model.getPatient());
		editedPatientEvaluation.setSpecialist(model.getSpecialist());
		editedPatientEvaluation.setEvaluation(model.getEvaluation());
	}

	/**
	 * Confirm save observation event.
	 */
	public void confirmSaveObservationEvent() {
		GeneralObservation editedObservation = model.getEditedObservation();
		editedObservation.setDate(new Date());
		model.getEditedEntity().getObservations().add(editedObservation);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#getEntityClass()
	 */
	protected Class<PatientEvaluation> getEntityClass() {
		return PatientEvaluation.class;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#getExportedFileName()
	 */
	@Override
	public String getExportedFileName() throws NotImplementedMethodException {
		String patientName = getModel().getEditedEntity().getPatient().getFullName();
		patientName = patientName.replace(",", "");
		patientName = patientName.replace(" ", "_");
		return "evaluacion_" + patientName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#getExportTemplate()
	 */
	@Override
	public String getExportTemplate() throws NotImplementedMethodException {
		return "appTemplates/patientEvaluationTemplate.html";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#getModel()
	 */
	@Override
	protected AbstractModel<PatientEvaluation> getModel() {
		return model;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#getService()
	 */
	@Override
	protected Service<PatientEvaluation> getService() {
		return service;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#init()
	 */
	public void init() {
		super.init();
		model.setFilter(new PatientEvaluationFilter());
		populateNonEntityPropertiesToModel();
	}

	/**
	 * OBSERVATION METHODS.
	 */

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#populateNonEntityPropertiesToModel()
	 */
	@Override
	public void populateNonEntityPropertiesToModel() {
		model.setSelectablePatients(patientService.getList(new PatientFilter()));
		model.setSelectableSpecialists(specialistService.getList(new SpecialistFilter()));
	}

	/**
	 * Prepare entity render.
	 * 
	 * @param uid
	 *            the uid
	 * @param useCaseMode
	 *            the use case mode
	 * @param patient
	 *            the patient
	 * @throws InstantiationException
	 *             the instantiation exception
	 * @throws IllegalAccessException
	 *             the illegal access exception
	 */
	public void prepareEntityRender(String uid, UseCaseMode useCaseMode, Patient patient) throws InstantiationException, IllegalAccessException {
		super.prepareEntityRender(uid, useCaseMode);
		if (patient != null) {
			model.setPatient(patient);
		}
	}
}
